# app/routes/hardware-linux.py
from flask import Blueprint, jsonify
import subprocess

hardware_bp = Blueprint('hardware', __name__)

@hardware_bp.route('/hardware_info', methods=['GET'])
def get_hardware_info():
    try:
        result = subprocess.run(['sudo', 'dmidecode'], stdout=subprocess.PIPE)
        return result.stdout.decode('utf-8')       
        hardware_info = {}

        # 硬盘信息
        disks = []
        for diskinfo in WMI.Win32_DiskDrive():
            if '虚拟' not in diskinfo.Caption:
                disks.append({
                    '制造商': diskinfo.Manufacturer,
                    '序列号': diskinfo.SerialNumber,
                    '硬盘大小': diskinfo.Size
                })
        hardware_info['disks'] = disks

        # CPU信息
        cpus = []
        for cpuinfo in WMI.Win32_Processor():
            cpus.append({
                '名称': cpuinfo.Name,
                '制造商': cpuinfo.Manufacturer,
                'ID号': cpuinfo.ProcessorId,
                '序列号': cpuinfo.SerialNumber,
                '核心数': cpuinfo.NumberOfCores
            })
        hardware_info['cpus'] = cpus

        # 主板信息
        boards = []
        for boardinfo in WMI.Win32_BaseBoard():
            boards.append({
                '制造商': boardinfo.Manufacturer,
                '产品号': boardinfo.Product,
                '序列号': boardinfo.SerialNumber
            })
        hardware_info['boards'] = boards

        # 网卡信息
        network_adapters = []
        for NetworkAdapter in WMI.Win32_NetworkAdapter():
            if NetworkAdapter.MACAddress:
                network_adapters.append({
                    '名称': NetworkAdapter.Name,
                    '制造商': NetworkAdapter.Manufacturer,
                    'MAC地址': NetworkAdapter.MACAddress
                })
        hardware_info['network_adapters'] = network_adapters

        # BIOS信息
        bioses = []
        for biosinfo in WMI.Win32_BIOS():
            bioses.append({
                '名称': biosinfo.Name,
                '制造商': biosinfo.Manufacturer,
                '序列号': biosinfo.SerialNumber
            })
        hardware_info['bioses'] = bioses

        # 内存信息
        memories = []
        for memoryinfo in WMI.Win32_PhysicalMemory():
            memories.append({
                '制造商': memoryinfo.Manufacturer,
                '部件号': memoryinfo.PartNumber,
                '序列号': memoryinfo.SerialNumber,
                '内存大小': memoryinfo.Capacity
            })
        hardware_info['memories'] = memories

        return jsonify(hardware_info), 200
    except Exception as e:
        return jsonify({'error': str(e)}), 500